package com.oceanstar.mapper;

import com.oceanstar.Pojo.AlertHistoryDTO;
import com.oceanstar.Pojo.AlertPendingDTO;
import com.oceanstar.Pojo.AlertInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface AlertMapper {
    // 查询报警历史
    @Select("SELECT " +
            "alert_id as alertId, " +
            "elderly_id as elderlyId, " +
            "name as name, " +
            "type as type, " +
            "status as status, " +
            "location as location, " +
            "alert_time as alertTime, " +
            "handle_time as handleTime, " +
            "handler as handler, " +
            "result as result, " +
            "description as description, " +
            "video_url as videoUrl, " +
            "photo_url as photoUrl " +
            "FROM alertinfo_table " +
            "WHERE status IN ('已处理', '处理中') " +
            "AND (#{gridId} IS NULL OR grid_id = #{gridId}) " +
            "AND (#{elderlyId} IS NULL OR elderly_id = #{elderlyId}) " +
            "AND (#{beginTime} IS NULL OR alert_time >= #{beginTime}) " +
            "AND (#{endTime} IS NULL OR alert_time <= #{endTime}) " +
            "ORDER BY alert_time DESC " +
            "LIMIT #{offset}, #{pageSize}")
    List<AlertHistoryDTO> selectAlertHistory(@Param("gridId") Integer gridId,
                                             @Param("elderlyId") Integer elderlyId,
                                             @Param("beginTime") String beginTime,
                                             @Param("endTime") String endTime,
                                             @Param("offset") Integer offset,
                                             @Param("pageSize") Integer pageSize);

    // 统计报警历史数量
    @Select("SELECT COUNT(*) " +
            "FROM alertinfo_table " +
            "WHERE status IN ('已处理', '处理中') " +
            "AND (#{gridId} IS NULL OR grid_id = #{gridId}) " +
            "AND (#{elderlyId} IS NULL OR elderly_id = #{elderlyId}) " +
            "AND (#{beginTime} IS NULL OR alert_time >= #{beginTime}) " +
            "AND (#{endTime} IS NULL OR alert_time <= #{endTime})")
    Long countAlertHistory(@Param("gridId") Integer gridId,
                           @Param("elderlyId") Integer elderlyId,
                           @Param("beginTime") String beginTime,
                           @Param("endTime") String endTime);

    // 查询待处理报警
    @Select("SELECT " +
            "alert_id as alertId, " +
            "elderly_id as elderlyId, " +
            "name as name, " +
            "type as type, " +
            "status as status, " +
            "location as location, " +
            "alert_time as alertTime, " +
            "description as description, " +
            "video_url as videoUrl " +
            "FROM alertinfo_table " +
            "WHERE status = '待处理' " +
            "AND grid_id = #{gridId} " +
            "ORDER BY alert_time DESC " +
            "LIMIT #{offset}, #{pageSize}")
    List<AlertPendingDTO> selectPendingAlerts(@Param("gridId") Integer gridId,
                                              @Param("offset") Integer offset,
                                              @Param("pageSize") Integer pageSize);

    // 统计待处理报警数量
    @Select("SELECT COUNT(*) " +
            "FROM alertinfo_table " +
            "WHERE status = '待处理' " +
            "AND grid_id = #{gridId}")
    Long countPendingAlerts(@Param("gridId") Integer gridId);

    // 处理报警 - 更新报警信息
    @Update("UPDATE alertinfo_table SET " +
            "status = #{status}, " +
            "result = #{result}, " +
            "photo_url = #{photoUrl}, " +
            "handle_time = NOW(), " +
            "handler = #{handler} " +
            "WHERE alert_id = #{alertId} " +
            "AND grid_id = #{gridId}")
    void handleAlert(@Param("alertId") Integer alertId,
                    @Param("status") String status,
                    @Param("result") String result,
                    @Param("photoUrl") String photoUrl,
                    @Param("handler") String handler,
                    @Param("gridId") Integer gridId);

    // 根据报警ID查询报警信息
    @Select("SELECT * FROM alertinfo_table WHERE alert_id = #{alertId}")
    AlertInfo selectAlertById(@Param("alertId") Integer alertId);

}
